package com.tsyun.view_project;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.res.AssetManager;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;

public class CalculateActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculate);
        TextView tvDemo = findViewById(R.id.tv_demo);
        SpannableString spannableString = new SpannableString("I'm a Chinese!");
//        ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#FF0000"));
//        spannableString.setSpan(span,0,4,SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
        ClickableSpan span = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Toast.makeText(CalculateActivity.this,"点击了我",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);
            }
        };
        AssetManager assetManager = getAssets();
        try {
            InputStream stream = assetManager.open("index.html");
            tvDemo.setText(new String());
        } catch (IOException e) {
            e.printStackTrace();
        }
        tvDemo.setMovementMethod(LinkMovementMethod.getInstance());
        spannableString.setSpan(span,0,4,SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
        tvDemo.setText(spannableString);
    }
}